﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Meqs.Entity;
using Meqs.Logic.Interface;
using System.Collections.Generic;

public partial class Credit_Teacher_RuleSettings_SettingClassEval : System.Web.UI.Page
{
    protected int currHistoryId = 0, currCauseId = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string s = Request.Params["historyId"];
            int.TryParse(s, out currHistoryId);

            s = "1";
            int.TryParse(s, out currCauseId);
        }
    }


    protected void btnTypeBind_Click(object sender, EventArgs e)
    {
        int historyId = 0, causeId = 0;
        string s = "";

        s = txtHistoryId.Text;
        if (!string.IsNullOrEmpty(s))
            int.TryParse(s, out historyId);
        currHistoryId = historyId;



    }
    protected void btnItemBind_Click(object sender, EventArgs e)
    {
        int historyId = 0, typeId = 0, schoolYear = 0, term = 0; ;
        string s = "";

        s = txtTypeId.Text;
        if (!string.IsNullOrEmpty(s))
            int.TryParse(s, out typeId);

        s = txtHistoryId.Text;
        if (!string.IsNullOrEmpty(s))
            int.TryParse(s, out historyId);



        SchoolInfo si = SchoolCommon.Sch_SchoolInfo;
        schoolYear = si.SchoolYear;
        term = si.SchoolTerm;

        Hashtable table = new Hashtable();
        table.Add("SchoolMark", Common.GetConstSchoolMark());
        table.Add("TypeID", typeId);


        IClassEvalItemLogic logic = Container<IClassEvalItemLogic>.Instance;

        List<ClassEvalItem> list = (List<ClassEvalItem>)logic.FindAll(table);

        currHistoryId = historyId;

        Repeater1.DataSource = list;
        Repeater1.DataBind();
    }

    /// <summary>
    /// 获取causeEvalItem列表
    /// </summary>
    /// <param name="historyId"></param>
    /// <param name="causeId"></param>
    /// <returns></returns>
    [System.Web.Services.WebMethod]
    public static List<ClassEvalType> GetTypeListByCauseId(int historyId)
    {
        Hashtable table = new Hashtable();
        table.Add("SchoolMark", Common.GetConstSchoolMark());
        table.Add("HistoryID", historyId);
        List<ClassEvalType> list = new List<ClassEvalType>();

        IClassEvalTypeLogic logic = Container<IClassEvalTypeLogic>.Instance;
        list = (List<ClassEvalType>)logic.FindAll(table);
        return list;
    }

    ///// <summary>
    ///// 获取学校的所有有效课程
    ///// </summary>
    ///// <returns></returns>
    //private List<GlobalCauseInfo> SelectSchoolCuase()
    //{
    //    IGlobalCauseInfoLogic logic = Container<IGlobalCauseInfoLogic>.Instance;
    //    List<GlobalCauseInfo> list = ( List<GlobalCauseInfo>) logic.SelectSchoolCause(Common.GetConstSchoolMark(), 1);
    //    return list;
    //}


    /// <summary>
    /// 获取学分评估的类型和规则信息
    /// </summary>
    /// <param name="historyId"></param>
    /// <param name="causeId"></param>
    /// <param name="typeId"></param>
    /// <returns></returns>
    private List<ClassEvalItem> GetEvalList(int historyId, int causeId, int typeId)
    {
        string schoolMark = Common.GetConstSchoolMark();

        IClassEvalItemLogic logic = Container<IClassEvalItemLogic>.Instance;
        Hashtable ht = new Hashtable();
        ht.Add("SchoolMark", schoolMark);
        ht.Add("TypeID", typeId);
        List<ClassEvalItem> list = (List<ClassEvalItem>)logic.FindAll(ht);
        return list;
    }


    #region 类型的增删改

    /// <summary>
    /// 验证类型名是否已被使用
    /// </summary>
    /// <param name="historyId"></param>
    /// <param name="causeId"></param>
    /// <param name="typeName"></param>
    /// <returns>0-未被使用，大于0被使用</returns>
    [System.Web.Services.WebMethod]
    public static int ValidateTypeName(int historyId, string typeName)
    {
        int n = 0;
        IClassEvalTypeLogic logic = Container<IClassEvalTypeLogic>.Instance;
        n = logic.ValidateTypeName(Common.GetConstSchoolMark(), historyId, typeName);
        return n;
    }

    [System.Web.Services.WebMethod]
    public static int AddType(int historyId, string typeName)
    {
        IClassEvalTypeLogic logic = Container<IClassEvalTypeLogic>.Instance;
        int n = 0;
        if (historyId > 0)
        {
            //已存在于history表的
            n = logic.InsertType(Common.GetConstSchoolMark(), historyId, typeName);
        }

        return n;
    }


    [System.Web.Services.WebMethod]
    public static int DelTypeById(int historyId, int typeId)
    {

        SchoolInfo si = SchoolCommon.Sch_SchoolInfo;

        IClassEvalTypeLogic logic = Container<IClassEvalTypeLogic>.Instance;
        Hashtable ht = new Hashtable();
        ht.Add("SchoolMark", si.SchoolMark);
        ht.Add("RID", typeId);
        return logic.Delete(ht);
    }

    [System.Web.Services.WebMethod]
    public static int UpdateType(int typeId, string typeName)
    {
        SchoolInfo si = SchoolCommon.Sch_SchoolInfo;
        IClassEvalTypeLogic logic = Container<IClassEvalTypeLogic>.Instance;
        Hashtable ht = new Hashtable();
        ht.Add("SchoolMark", si.SchoolMark);
        ht.Add("RID", typeId);
        ht.Add("Caption", typeName);
        return logic.UpdateTypeName(ht);
    }

    #endregion

    protected string GetTypeOpt(int type)
    {
        if (type < 1 || type > 2)
            return "";

        string s = "";
        if (type == 1)
        {
            s = "<option value='1' selected='selected'>计分</option>" +
            "  <option value='2'>计次</option>";
        }
        if (type == 2)
        {
            s = "<option value='1'>计分</option>" +
           "  <option value='2' selected='selected'>计次</option>";
        }
        return s;
    }

    protected string GetOperatorOpt(int nOperator)
    {
        string s = "";
        if (nOperator == 1)
        {
            s = "<option value='1' selected='selected'>加分</option>" +
              "<option value='-1'>减分</option>";
        }
        else
        {
            s = "<option value='1'>加分</option>" +
             "<option value='-1' selected='selected'>减分</option>";
        }
        return s;
    }

    protected string GetDisplay(int itemType)
    {
        string s = "";
        if (itemType == 1)
        {
            s = "none";
        }
        else
        {
            s = "block";
        }
        return s;
    }



    [System.Web.Services.WebMethod]
    public static int SaveCauseEvalItem(ClassEvalItem[] list, ClassEvalItem[] delIds)
    {
        List<ClassEvalItem> listDel = new List<ClassEvalItem>();
        List<ClassEvalItem> listAdd = new List<ClassEvalItem>();
        foreach (ClassEvalItem item in list)
        {
            listAdd.Add(item);
            if (item.RID > 0)
                listDel.Add(item);
        }

        if (delIds.Length > 0)
        {
            foreach (ClassEvalItem item2 in delIds)
            {
                listDel.Add(item2);
            }
        }

        IClassEvalItemLogic logic = Container<IClassEvalItemLogic>.Instance;
        int n = logic.InsertCauseItem(Common.GetConstSchoolMark(), listDel, listAdd);

        return n;
    }

    #region 绑定数据后 需要处理的前台效果 比如选中状态

    //做个点击效果
    protected void SetTypeArr()
    {
        Response.Write("currTypeId = 0;");
        Response.Write("currTypeName = \"\";");
        Response.Write("var ul = document.getElementById('ul');");
        Response.Write("var listA = document.getElementsByTagName('a');");
        Response.Write("if(listA.length > 0)");
        Response.Write("{");
        Response.Write("    for(var i =0;i < listA.length;i++)");
        Response.Write("    {");
        Response.Write("        if(i==0)");
        Response.Write("        {");
        Response.Write("            listA[i].click();");
        Response.Write("            currTypeId = listA[i].id;");
        Response.Write("            currTypeName = listA[i].Text;");
        Response.Write("        }");
        Response.Write("    }");
        Response.Write("}");

    }

    #endregion
}
